package com.example.demo.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.Entity.Disease;
import com.example.demo.Entity.Question;
import com.example.demo.Service.QuestionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/questionlist")
    public String questionlist(Model model, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "15")Integer pageSize){

        Page<Object> objects = PageHelper.startPage(pageNum, pageSize);
        List<Question> questions=questionService.findAll();
        PageInfo<Question> pageInfo=new PageInfo<Question>(questions);
        //pageInfo.setList(questions);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("pageNum",pageInfo.getPageNum());
        model.addAttribute("pageSize",pageInfo.getPageSize());
        model.addAttribute("totalPages",pageInfo.getPages());
        model.addAttribute("isFirstPage",pageInfo.isIsFirstPage());
        model.addAttribute("isLastPage",pageInfo.isIsLastPage());
        return "questionlist";
    }

    @RequestMapping("/question")
    public String question(Model model,@RequestParam(defaultValue = "4412")Integer id){
        Question question=questionService.findById(id);
        model.addAttribute("question",question);
        return "question";
    }


}
